from django.shortcuts import render, redirect
from django.views import View
from miniweb.models import StockInfo, FocusInfo
from django import http


# Create your views here.
class MiniwebIndexView( View ):
	# http://127.0.0.1:8000/index/
	def get(self, request):
		info = StockInfo.objects.all()

		context = {
			'infos': info
		}

		return render( request, 'index.html', context )


class MiniwebCenterView( View ):

	def get(self, request):
		codes = FocusInfo.objects.all()
		# note_info = FocusInfo.objects.all()

		info_list = []
		for i in codes:
			info = StockInfo.objects.filter( code=i.code )[0]
			info.note_info = i.note_info
			# print( info )
			# print( info.code )
			# info.code
			info_list.append( info )
		# for i in note_info:
		# 	note_info = FocusInfo.objects.filter( note_info=i.note_info )[0]
		# 	info_list.append( note_info )

		print( info_list )

		context = {
			'infos': info_list
		}
		# for i in info_list:
		# 	print( i.code )

		return render( request, 'center.html', context )


class MiniwebaddView( View ):

	def get(self, request, code):
		# print( code )
		info = FocusInfo.objects.all()
		code_list = []
		for i in info:
			code_list.append( i.code )
		if code not in code_list:
			FocusInfo.objects.create(
				code=code,
				note_info=''
			)
			# info = FocusInfo.objects.all()
			# print( info )
			dick = {
				'data': '关注成功',
				'status': '添加成功'
			}
		else:
			dick = {
				'data': '信息已存在',
				'status': '添加失败'
			}

		return http.JsonResponse( dick )


class MiniwebdelView( View ):

	def get(self, request, code):
		FocusInfo.objects.filter( code=code ).delete()
		dick = {
			'data': '删除成功',
			'status': 'ok'
		}

		return http.JsonResponse( dick )


class MiniwebupdateView( View ):

	def get(self, request, code):
		info = FocusInfo.objects.filter( code=code )

		context = {
			'infos': info
		}
		return render( request, 'update1.html', context )


class MiniwebMakeView( View ):

	def get(self, request, code, note_info):
		print( code, note_info )
		info = FocusInfo.objects.filter( code=code ).update( note_info=note_info )
		dick = {
			'data': '修改成功',
			'status': 'ok'
		}
		return http.JsonResponse(dick)
